﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Sencha.Serialization;

namespace Sencha.Direct
{
    public abstract partial class Router
    {
        [DataContract]
        public class IncomingRequest
        {
            public const string RequestFormTransactionId = "extTID";
            public const string RequestFormAction = "extAction";
            public const string RequestFormMethod = "extMethod";
            public const string RequestFormUpload = "extUpload";
            public const string RequestFormType = "extType";

            [DataMember(Name = "action")]
            public string Controller
            {
                get;
                set;
            }

            [DataMember(Name = "method")]
            public string Action
            {
                get;
                set;
            }

            [DataMember(Name = "data")]
            public IDictionary<string, string> Arguments
            {
                get;
                set;
            }

			// obsolete in sencha 4.0
			//[DataMember(Name = "params")]
			//public string[] ParameterNames
			//{
			//    get;
			//    set;
			//}

            [DataMember(Name = "type")]
            public string Type
            {
                get;
                set;
            }

            [IgnoreDataMember]
            public bool IsForm
            {
                get;
                set;
            }

            [IgnoreDataMember]
            public bool IsUpload
            {
                get;
                set;
            }

            [DataMember(Name = "tid")]
            public int TransactionId
            {
                get;
                set;
            }

            public static IncomingRequest Capture(System.Web.HttpContextBase httpContextBase)
            {
                IncomingRequest request = new IncomingRequest();
                request.Controller = httpContextBase.Request[IncomingRequest.RequestFormAction] ?? string.Empty;
                request.Action = httpContextBase.Request[IncomingRequest.RequestFormMethod] ?? string.Empty;
                request.Type = httpContextBase.Request[IncomingRequest.RequestFormType] ?? string.Empty;
                request.IsUpload = Convert.ToBoolean(httpContextBase.Request[IncomingRequest.RequestFormUpload]);
                request.IsForm = true;
                request.TransactionId = Convert.ToInt32(httpContextBase.Request[IncomingRequest.RequestFormTransactionId]);

                var data = new Dictionary<string, string>();
                foreach (string key in httpContextBase.Request.Form.Keys)
                {
                    if (key == RequestFormTransactionId || key == RequestFormAction || key == RequestFormMethod
                        || key == RequestFormType || key == RequestFormUpload)
                        continue;

                    data.Add(key, httpContextBase.Request.Form[key]);
                }
				request.Arguments = data;

                return request;
            }
        }
    }
}
